import { wxRequest } from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    list: [],
    hasNextPage: false,
    keyword: '',
    pageData: {}
  },

  // onShow: function () {
  //   this.getlist()
  // },
  onLoad: function () {
    this.getlist()
  },
  //查询列表
  getlist: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/comm/beanCrud/cms.AdPictureVO/find',
      method: 'POST',
      data: {
        keyword: this.data.keyword
      }
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      this.setData({
        list: list,
        pageData: res.page,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

      console.log(res)

    })
  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },
  
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },



  //编辑
  handleEdit: function (e) {
    var id = e.currentTarget.dataset.id

    var item = e.currentTarget.dataset.item
    wx.setStorageSync('adpicItem', item)

    wx.navigateTo({
      url: '/pages/publish/adpicture/index?id=' + id,
    })
  },


  //删除
  handlDelet: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '确定删除该内容',
      success: function (res) {
        if (res.confirm) {
          that.delectRequest(id)
        }
      }
    })
  },

  add:function(){
    wx.navigateTo({
      url: '/pages/publish/adpicture/index',
    })
  },
  delectRequest: function (id) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/sys/protected/comm/beanCrud/cms.AdPictureVO/delete",
      method: 'POST',
      data: { id }
    }, (res) => {
      console.log(res)
      wx.showToast({
        title: "删除成功",
      })
      that.getlist()
    })
  }



})